import { useState, useEffect } from 'react';
// @ts-ignore
import emitter from '#/utils/events';

export type IThemeType = 'gre' | 'dark' | 'light';

export function useThemeChange() {
  const [theme, setTheme] = useState<IThemeType>(
    (window.localStorage.getItem('saas-theme') as IThemeType) || 'light',
  );
  useEffect(() => {
    emitter.addListener('onGlobalThemeChange', (e: any) => {
      setTheme(e.key);
    });
    return () => {
      emitter.removeListener('onGlobalThemeChange', () => {});
    };
  }, []);

  return { theme };
}
